﻿namespace mgen_commonDlg
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls.Primitives;

    using Microsoft.Win32;

    [TemplatePart(Name = PART_Button, Type = typeof(ButtonBase))]
    public class SaveFileControl : CommonDialogControl
    {
        #region Fields

        public static readonly DependencyProperty DefaultExtensionProperty = 
            DependencyProperty.Register("DefaultExtension", typeof(string), typeof(SaveFileControl),
                new FrameworkPropertyMetadata((string)null));

        #endregion Fields

        #region Constructors

        static SaveFileControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SaveFileControl), new FrameworkPropertyMetadata(typeof(SaveFileControl)));
        }

        #endregion Constructors

        #region Methods

        public override sealed void Browse()
        {
            var dlg = new SaveFileDialog();
            dlg.Filter = Filter;
            dlg.FileName = Path;

            var res = dlg.ShowDialog();
            OnShown(res);
            if (res == true)
            {
                Path = dlg.FileName;
                OnOpened();
            }
        }

        #endregion Methods
    }
}